﻿<?php
include_once("./auth.php");
include_once("./database/dbutil.php");

$dbUtil = new DBUtil();

if(isset($_GET['building_id'])) {
	$buildingId = $_GET['building_id'];
}
else {
	$buildingId = 0;
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Lightcontrol</title>

    <!-- Bootstrap libraries -->
    <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../js/list.js"></script>
    
	<!-- Custom styles for this template -->
   
    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
     <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Lightcontrol</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li><a id="index" href="index.php">Home</a></li>
             <li class="dropdown active">
				<a href="#" class="dropdown-toggle" data-toggle="dropdown">Electronics<b class="caret"></b></a>
				<ul class="dropdown-menu">
					<li><a id="buildings" href="buildings.php">Geb&auml;ude</a></li>
					<li><a id="rooms" href="rooms.php">R&auml;ume</a></li>
					<li><a id="plugs" href="plugs.php">Steckdosen</a></li>
				</ul>
			</li>
            <li><a id="contact" href="contact.php">Kontakt</a></li>
          </ul>
		   <ul class="nav navbar-nav navbar-right">
				<?php if($_SESSION['loggedIn']){ ?>
					<li><p class="navbar-text"><?php echo "Logged in as ".$_SESSION['username']; ?> </p></li>
					<li><a id="logout" href="logout.php">Logout</a></li>
				<?php }else{ ?>
					<li><a id="login" href="login.php">Login</a></li>
				<?php } ?>
				
		   </ul>
        </div><!--/.nav-collapse -->
      </div>
	</div>
    <div class="container" style="margin-top:50px">
      <div class="starter-template">
		<h1>R&auml;ume</h1>
		<div id="rooms-table">
		  <input class="search" placeholder="Suche..." />
		  <table class="table table-hover">
			<thead>
			  <tr>
			    <td>ID</td>
			  	<td>Name</td>
				<td>Geb&auml;ude</td>
				<td>Status</td>
				<td>Verbrauch</td>
				<td>Anzahl Steckdosen</td>
			  </tr>
			</thead>
			<tbody class="list">
			<?php 
				foreach($dbUtil -> getRooms($buildingId) as $room) {
					if($room['status'] == 0) { $statusLabel = "<span class=\"label label-danger statusbutton\">Off</span>"; }
					if($room['status'] == 1) { $statusLabel = "<span class=\"label label-success statusbutton\">On</span>";}

					echo "<tr>" .
						 	"<td>" . "<a href=\"./plugs.php?room_id=" . $room['id'] ."\">". $room['id'] . "</a>" . "</td>" .
    						"<td class=\"room\">" . $room['name'] . "</td>" .
    						"<td class=\"building\">" . $room['building_name'] . "</td>" .
							"<td>" . $statusLabel . "</td>" .
							"<td class=\"power\">" . $room['power_consumption'] . " " . $room['unit'] . "</td>" .
    						"<td>" . $room['plug_count'] . "</td>" .
    					 "</tr>";
				}
				?>
			<tbody>
		  </table>
		</div>
      </div>
    </div><!-- /.container -->
  </body>
  <script>
	$( document ).ready(function() {
		var options = {
				  valueNames: [ 'room', 'building', 'power' ]
		};
		var roomList = new List('rooms-table', options);
		
		$('.statusbutton').css( 'cursor', 'pointer' );
		$(".statusbutton").click(function() {
			if($(this).hasClass("label-success")){
				$(this).attr("class","label label label-danger statusbutton");
				$(this).text("Off");
			}else{
				$(this).attr("class","label label label-success statusbutton");
				$(this).text("On");
			}	
		});
	});
  </script>
</html>
